require("../css/course.less")
document.ready(function () {
    window.$utils.tabbar("course")
    let mainCount = document.querySelector("#mainCount")

    let userId = JSON.parse(localStorage.getItem("user")).userId
    // 页面打开时获取当前用户的课程列表
    axios.get(window.$utils.url + `/sports/courseList?id=${userId}`)
        .then(function (res) {
            let ret = res.data
            if (ret.status == 0) {
                let retArr = ret.data
                console.log(retArr);
                let newCourse = retArr.find(function (item) {
                    // console.log(item);
                    return item.latest == 1
                })
                newCour(newCourse)
                // console.log(newCourse);
                let oldCourse=retArr.filter(function(item){
                    return item.latest==0
                })
                oldCour(oldCourse)
            }
        })
        .catch(function (err) {
            console.log(err);
        })

    //渲染新课程
    function newCour(arr) {
        let str = ""
        str =
            `
        <p>最新课程</p>
        <a href="./introduce.html?${arr.courseId}" class="main-course-item mt20">
        <img class="course-img" src=${window.$utils.url+arr.imgurl} alt="">
        <h3 class="title fs16 mt8">${arr.name}</h3>
        <p class="desc fs12 mt8">${arr.desc}</p>
        </a>
        `
        mainCount.innerHTML=str
    }

    // 渲染旧课程
    function oldCour(arr){
        let str=""
        str=`<p>课程</p>`
        arr.forEach(function(arr){
            str+=`

                <a href="./introduce.html?${arr.courseId}" class="main-course-item mt20">
                    <img class="course-img" src=${window.$utils.url+arr.imgurl} alt="">
                    <h3 class="title fs16 mt8">${arr.name}</h3>
                    <p class="desc fs12 mt8">${arr.desc}</p>
                </a>
            `
        })
        document.querySelector("#mainCourse").innerHTML=str
    }

})